#include <QDebug>
#include "Shape.h"
#include "SettingHelper.h"

IShape::IShape(float h, float v)
{
	m_fPixelPitchH = h;
	m_fPixelPitchV = v;
}

void IShape::addPoint(QPoint pt1)
{
	int count = m_listPt.count();

	if(count < 2) m_listPt << pt1;
	else if(count == 2) m_listPt[1] = pt1;
	else {}
}

int IShape::size() const
{
	return m_listPt.count() == 2 ? (m_listPt[0] - m_listPt[1]).manhattanLength() : 0;
}

float IShape::realSize() const
{
	float fSize(0.f);
	if(m_listPt.count() == 2)
	{
		QPointF pt = m_listPt[0] - m_listPt[1];

		pt.setX(pt.x() * m_fPixelPitchH);
		pt.setY(pt.y() * m_fPixelPitchV);
		fSize = pt.manhattanLength();
	}

	return fSize;
}

void Oval::draw(QPainter &painter, float fRatio)
{
	if(m_listPt.count() == 2)
	{
		painter.save();
		painter.setPen(QPen(Qt::red, 4));
		painter.drawEllipse(QRect(m_listPt[0] * fRatio, m_listPt[1] * fRatio).normalized());
		painter.restore();
	}
}

void Rect::draw(QPainter &painter, float fRatio)
{
	if(m_listPt.count() == 2)
	{
		painter.save();
		painter.setPen(QPen(Qt::red, 4));
		painter.drawRect(QRect(m_listPt[0] * fRatio, m_listPt[1] * fRatio).normalized());
		painter.restore();
	}
}

void Line::draw(QPainter &painter, float fRatio)
{
	if(m_listPt.count() == 2)
	{
		QPointF pt1 = m_listPt[0] * fRatio;
		QPointF pt2 = m_listPt[1] * fRatio;

		painter.save();
		painter.setPen(QPen(Qt::red, 4));
		painter.drawLine(pt1, pt2);
		painter.drawPoint(pt1);
		painter.drawPoint(pt2);
		painter.restore();
	}
}

void Pen::draw(QPainter &painter, float fRatio)
{
	const QPolygonF poly = this->makePolygon(fRatio);

	painter.save();
	painter.setPen(QPen(Qt::red, 4));
	painter.drawPolyline(poly);
	painter.restore();
}

void Pen::addPoint(QPoint pt1)
{
	m_listPt << pt1;
}

int Pen::size() const
{
	const QPolygonF poly = this->makePolygon(1.f); //real size
	QRect rect = poly.boundingRect().toRect();

	return (rect.topLeft() - rect.bottomRight()).manhattanLength();
}

const QPolygonF Pen::makePolygon(float fRatio) const
{
	QPolygonF poly;

	foreach (const QPointF pt, m_listPt)
	{
		poly << pt * fRatio;
	}

	return poly;
}


void Text::draw(QPainter &painter, float fRatio)
{
	if(!m_strText.isNull())
	{
		painter.drawText(m_listPt[0] * fRatio, m_strText);
	}
}

void Text::addPoint(QPoint pt1)
{
	if(m_listPt.count() == 0) m_listPt << pt1;
}

void Text::setText(const QString &strText)
{
	m_strText = strText;
}

int Text::size() const
{
	return !m_strText.trimmed().isNull() ? 10 : 0;
}

void Length::draw(QPainter &painter, float fRatio)
{
	Line::draw(painter, fRatio);

	if(m_listPt.count() == 2) painter.drawText(m_listPt[0] * fRatio, QString("%1mm").arg(this->realSize()));
}

void Profile::draw(QPainter &painter, float fRatio)
{
	Line::draw(painter, fRatio);

	if(m_listPt.count() == 2) painter.drawText(m_listPt[0] * fRatio, QString("%1mm").arg(Length::realSize()));
}

int Profile::size() const
{
	return 1;
}

const QList<QPoint> Profile::pointList()
{
	QList<QPoint> list;
	int iRealSize = Length::size();

	if(m_listPt.count() == 2 && iRealSize > 0)
	{
		QLineF line(m_listPt[0], m_listPt[1]);

		for (int var = 0; var <= iRealSize; ++var)
		{
			list << line.pointAt(1.f * var / iRealSize).toPoint();
		}
	}

	return list;
}

void Position::draw(QPainter &painter, float fRatio)
{
	if(m_listPt.count() == 1)
	{
		QPoint ptReal = m_listPt[0];
		QPoint pt = ptReal * fRatio;
		{
			painter.save();
			painter.setPen(QPen(Qt::red, 4));
			painter.drawPoint(pt);
			painter.restore();
		}
		painter.drawText(pt, QString("(%1,%2)").arg(ptReal.x()).arg(ptReal.y()));
	}
}

void Position::addPoint(QPoint pt1)
{
	if(m_listPt.count() == 0) m_listPt << pt1;
}

int Position::size() const
{
	return 10;
}
